package com.zmzncs.lmtc.common.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class ShortMsgUtil {

    private static DefaultProfile profile = null;
    private static IAcsClient client = null;

    private static DefaultProfile getProfile(){
        if(profile == null){
            profile = DefaultProfile.getProfile("cn-hangzhou", "LTAIXNh9hAX6xGIW", "LXiaCNfnNLqKk0THc8VdjKJ4J6HjVz");
        }
        return profile;
    }
    private static IAcsClient getAcsClient(){
        if(client==null){
            client= new DefaultAcsClient(getProfile());
        }
        return client;
    }

    /**
     * 发送短信验证码
     * @param templateCode
     * @param phoneNumber
     * @param templateParam
     */
    public static Map sendVerifyCode(String templateCode, String phoneNumber, Map<String, Object> templateParam) {
        //构建请求
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        //  自定义模板信息
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", "中萌智能城市");
        request.putQueryParameter("TemplateCode", templateCode);

        Map map = new HashMap();
        try {
            if(templateParam != null){
                request.putQueryParameter("TemplateParam", new ObjectMapper().writeValueAsString(templateParam));
            }

            //  发送短信
            CommonResponse response = getAcsClient().getCommonResponse(request);
            String jsonData = response.getData();
            log.info("发送短信验证码响应：{}", jsonData);
            map = new ObjectMapper().readValue(jsonData, Map.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 批量发送短信
     * @param phoneNumbers
     * @param templateCode
     * @return
     */
    public static Map sendBatchSms(List<String> phoneNumbers, List<String> areaCodes, String templateCode, List<Map<String, String>> templateParams) {
        String phoneNumbersStr = "";
        String templateParamsStr = "";
        String signNamesStr = "";

        try {
            List<String> signNames = new ArrayList<>();
            List<String> phoneNumberJson = new ArrayList<>();
            for (int i=0; i<phoneNumbers.size(); i++){
                signNames.add("中萌智能城市");
                phoneNumberJson.add(areaCodes.get(i) + phoneNumbers.get(i));
            }
            //  将对象转换成字符串
            signNamesStr = new ObjectMapper().writeValueAsString(signNames);
            phoneNumbersStr = new ObjectMapper().writeValueAsString(phoneNumberJson);
            templateParamsStr = new ObjectMapper().writeValueAsString(templateParams);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendBatchSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumberJson", phoneNumbersStr);
        request.putQueryParameter("SignNameJson", signNamesStr);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParamJson", templateParamsStr);

        Map map = new HashMap();
        try {
            CommonResponse response = getAcsClient().getCommonResponse(request);
            String jsonData = response.getData();
            log.info(jsonData);
            map = new ObjectMapper().readValue(jsonData, Map.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }

    /**
     * 查询短信模版
     * @param templateCode
     */
    public static String querySmsTemplate(String templateCode){
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("QuerySmsTemplate");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("TemplateCode", templateCode);

        //  模版内容
        String templateContent = "";
        try {
            CommonResponse response = getAcsClient().getCommonResponse(request);
            String jsonData = response.getData();
            log.info("短信验证码模版：{}", jsonData);
            Map<String, String> map = new ObjectMapper().readValue(jsonData, Map.class);
            templateContent = map.get("TemplateContent");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return  templateContent;
    }


    public static void main(String[] args) {
        log.info("{}", (int)((Math.random()*9+1)*1000));
    }
}
